﻿using System;
using System.Collections.Generic;
using System.Linq;

using FY2013.Model;
using System.Web.SessionState;
using System.Web;

namespace FY2013.Business
{
    public enum SessionType
    {
        Title,
        Paragraph
    }

    [Serializable]
    public class UserSession
    {
        public SessionType Type;
        public string Account;
        public List<decimal> IdList = new List<decimal>();
    }

    public class LoginException : Exception
    {
        public LoginException(string message)
            : base(message)
        {
 
        }
    }

    public static class SessionHelper
    {
        public static void Create(HttpSessionState session, decimal articleId, decimal? paragraphId)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
            {
                throw new LoginException("此操作不对游客开发，请登录后再重试！");
            }

            UserSession userSession = new UserSession();
            userSession.Type = paragraphId == null ? SessionType.Title : SessionType.Paragraph;
            userSession.IdList.Add(paragraphId ?? articleId);
            userSession.Account = HttpContext.Current.User.Identity.Name;

            session["UserSession"] = userSession;
            session.Timeout = (int)BusinessSettings.Default.SessionKeepTime.TotalMinutes;
        }

        public static void Delete(HttpSessionState session)
        {
            session.Abandon();
        }

        public static void AttachSession(HttpSessionState session, Entities entities)
        {
            if (entities == null)
            {
                return;
            }

            UserSession userSession = session["UserSession"] as UserSession;

            if (userSession == null)
            {
                return;
            }

            if (userSession.Type== SessionType.Title)
            {
                foreach (decimal item in userSession.IdList.Distinct())
                {
                    entities.ExecuteStoreCommand(string.Format("update {0} set SessionTime='{1}',SessionAccount='{2}' where ArticleID={3}", entities.Articles.EntitySet.ElementType.Name, DateTime.Now, userSession.Account,item));
                }
            }else if (userSession.Type == SessionType.Paragraph)     
            {
                foreach (decimal item in userSession.IdList.Distinct())
                {
                    entities.ExecuteStoreCommand(string.Format("update {0} set SessionTime='{1}',SessionAccount='{2}' where ParagraphID={3}", entities.Paragraphs.EntitySet.ElementType.Name, DateTime.Now, userSession.Account, item));
                }
            }
        }


        public static void DetachSession(HttpSessionState session, Entities entities)
        {
            if (entities == null)
            {
                return;
            }

            UserSession userSession = session["UserSession"] as UserSession;

            if (userSession == null)
            {
                return;
            }

            if (userSession.Type == SessionType.Title)
            {
                foreach (decimal id in userSession.IdList.Distinct())
                {
                    entities.ExecuteStoreCommand(string.Format("update {0} set SessionTime=NULL,SessionAccount=NULL where ActicleID={1}", entities.Articles.EntitySet.ElementType.Name,id));
                }
            }
            else if (userSession.Type== SessionType.Paragraph)
            {
                foreach (decimal id in userSession.IdList.Distinct())
                {
                    entities.ExecuteStoreCommand(string.Format("update {0} set SessionTime=NULL,SessionAccount=NULL where ParagraphID={1}", entities.Paragraphs.EntitySet.ElementType.Name, id));
                }
            }
            session["UserSession"] = null;
        }
    }
}
